package com.server.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.service.IAttachmentService;
import com.server.service.ICaseStudyService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("CaseStudyService")
public class CaseStudyService implements ICaseStudyService {

	private final String tableName = "education.case_warehouse";
	@Autowired
	private CommonDataService commonDataService;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IAttachmentService attachmentService;

	@Override
	public DataVector<Map<String, Object>> getList(String whereStr,
			String sortStr, RequestInput requestInput) {
		return commonDataService.getListData(tableName, whereStr, sortStr,
				requestInput);
	}

	@Override
	public Map<String, Object> caseDetail(Integer id) {
		String sql = "select * from %s where id=%d";
		List<Map<String, Object>> attaList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> vedioList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String
				.format(sql, tableName, id));
		if (list.size() > 0) {
			Map<String, Object> returnMap = list.get(0);
			attaList = attachmentService.getList("case", id.toString());
			returnMap.put("atta", attaList);
			vedioList = attachmentService.getList("case_video", id.toString());
			returnMap.put("atta_vedio", vedioList);
			
			/*Object injuredObj = returnMap.get("injured_num");
			Object deathObj = returnMap.get("death_num");
			
			String injNum = injuredObj==null ? "" : injuredObj.toString();
			String deathNum = deathObj==null ? "" : deathObj.toString();
			
			switch (injNum) {
			case "0":
				returnMap.put("injured_num", "无");
				break;
			case "1":
				returnMap.put("injured_num", "10人以下");
				break;
			case "20":
				returnMap.put("injured_num", "10-50人");
				break;
			case "60":
				returnMap.put("injured_num", "50-100人");
				break;
			case "110":
				returnMap.put("injured_num", "100人以上");
				break;
			default:
				break;

			}
			switch (deathNum) {
			case "0":
				returnMap.put("death_num", "无");
				break;
			case "1":
				returnMap.put("death_num", "10人以下");
				break;
			case "20":
				returnMap.put("death_num", "10-30人");
				break;
			case "40":
				returnMap.put("death_num", "30人以上");
				break;
			default:
				break;

			}*/
			return returnMap;
		}
		return new HashMap<String, Object>();
	}

	@Override
	public Boolean score(String operator, Map<String, Object> inputMap) {
		return commonDataService
				.add(operator, "education.case_score", inputMap);
	}

	@Override
	public Map<String, Object> getScore(String id) {
		Map<String, Object> scoreMap = new HashMap<String, Object>();
		// 查询评分个数
		String sql1 = "select count(*) from education.case_score where caseId=%s";
		// 查询实用性总分
		String sql2 = "select sum(utility) from education.case_score where caseId=%s";
		// 查询清晰度总分
		String sql3 = "select sum(clarity) from education.case_score where caseId=%s";
		// 查询启发性总分
		String sql4 = "select sum(heuristic) from education.case_score where caseId=%s";
		Integer count = jdbcTemplate.queryForObject(String.format(sql1, id),
				Integer.class);
		long count1 = (long) count;

		if (count == 0) {
			scoreMap.put("utility", 0);
			scoreMap.put("clarity", 0);
			scoreMap.put("heuristic", 0);
			scoreMap.put("total", 0);
			return scoreMap;
		}
		DecimalFormat fnum = new DecimalFormat("##0.0");
		double utility = (double) jdbcTemplate.queryForObject(
				String.format(sql2, id), Integer.class);
		double clarity = (double) jdbcTemplate.queryForObject(
				String.format(sql3, id), Integer.class);
		double heuristic = (double) jdbcTemplate.queryForObject(
				String.format(sql4, id), Integer.class);
		String utilityAver = fnum.format(utility / count1);
		String clarityAver = fnum.format(clarity / count1);
		String heuristicAver = fnum.format(heuristic / count1);
		float total = (float) (utility + clarity + heuristic);
		String totalAver = fnum.format(total / (count1 * 3));

		scoreMap.put("utility", utilityAver);
		scoreMap.put("clarity", clarityAver);
		scoreMap.put("heuristic", heuristicAver);
		scoreMap.put("total", totalAver);
		return scoreMap;
	}

}
